articles

home / developersection / articles / c# reflection – a complete beginner's guide

C# Reflection – A Complete Beginner's Guide

C# Reflection – A Complete Beginner's Guide

Anubhav Kumar 835 22-Apr-2025

Reflection in C# is a powerful feature that allows you to inspect, analyze, and even modify the structure of your code (like classes, methods, properties, etc.) at runtime. It’s part of the System.Reflection namespace.

What is Reflection?

Reflection allows your program to:

  1. Inspect assemblies
  2. Examine types (classes, interfaces, structs)
  3. Access metadata (attributes, method info)
  4. Dynamically create instances
  5. Invoke methods and access properties/fields

It's widely used in:

  1. Frameworks like Entity Framework, ASP.NET MVC
  2. Serialization/Deserialization
  3. Unit Testing
  4. Plugins and dynamic module loading

Namespaces Required

using System;
using System.Reflection;

Basic Example

using System;
using System.Reflection;

class Person
{
    public string Name { get; set; }

    public void SayHello()
    {
        Console.WriteLine("Hello!");
    }
}

class Program
{
    static void Main()
    {
        Type type = typeof(Person); // Get type info

        Console.WriteLine("Class Name: " + type.Name);

        Console.WriteLine("\nProperties:");
        foreach (PropertyInfo prop in type.GetProperties())
        {
            Console.WriteLine(prop.Name);
        }

        Console.WriteLine("\nMethods:");
        foreach (MethodInfo method in type.GetMethods())
        {
            Console.WriteLine(method.Name);
        }
    }
}

Common Reflection Classes

Class Description
Type Represents type declarations (class, struct, etc.)
PropertyInfo Represents a property
MethodInfo Represents a method
FieldInfo Represents a field
ConstructorInfo Represents a constructor
Assembly Represents an entire .NET assembly (DLL/EXE)

Getting Type Info

Type t1 = typeof(Person); // Compile time
Type t2 = obj.GetType();  // Runtime
Type t3 = Type.GetType("Namespace.Person");

Invoking a Method via Reflection

Person p = new Person();
MethodInfo method = typeof(Person).GetMethod("SayHello");
method.Invoke(p, null); // Calls SayHello on p

Accessing Fields & Properties

PropertyInfo prop = typeof(Person).GetProperty("Name");
prop.SetValue(p, "Alice");
Console.WriteLine(prop.GetValue(p)); // Output: Alice

Creating Objects Dynamically

Type type = typeof(Person);
object obj = Activator.CreateInstance(type); // Creates new Person()

MethodInfo method = type.GetMethod("SayHello");
method.Invoke(obj, null);

Reading Attributes

[Obsolete("Use NewClass instead")]
class OldClass { }

Type type = typeof(OldClass);
object[] attrs = type.GetCustomAttributes(false);
foreach (object attr in attrs)
{
    Console.WriteLine(attr);
}

Reflection Limitations

  1. Performance: Slower than direct access.
  2. Security: Can bypass encapsulation.
  3. Complexity: Harder to maintain/debug.

Summary

Concept Description
What is it? A way to inspect metadata at runtime
Key Namespace System.Reflection
Use Cases Serialization, DI, Frameworks, Testing
Main Class Type
Dynamic Method Call MethodInfo.Invoke()
Create Object Activator.CreateInstance()

Reflection gives C# incredible flexibility and power, especially for frameworks and tools. But use it wisely — only when necessary — to avoid complexity and performance issues.

Read More -


c# c# 
Updated 22-Apr-2025
Anubhav Kumar

Student

The Anubhav portal was launched in March 2015 at the behest of the Hon'ble Prime Minister for retiring government officials to leave a record of their experiences while in Govt service .

Leave Comment

Comments

Liked By